#plotting - manipulation checks
#alan yan
#2-9-22

####setup####
#clear environment
rm(list = ls())

#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       egg,
       car,
       boot,
       grid,
       gridExtra,
       ggpubr,
       Cairo)

#### FUNCTIONS ####

# ggplot theme
theme_shom_alt <- function (base_size = 11, waffle = FALSE) 
{
  ret <- theme_minimal(base_size = base_size, base_family = "Roboto Condensed"
  ) + 
    theme(plot.background = element_rect(fill = "#f5f5f2", 
                                         color = NA), 
          panel.background = element_rect(fill = "#f5f5f2",color = NA), 
          legend.background = element_rect(fill = "#f5f5f2",color = NA))
  if (waffle) {
    ret + theme(axis.text = element_blank())
  }
  else {
    ret
  }
}

#plotting 
plot.avg <- function(data) {
  ggplot(data, aes(x = exp.condition, y = statistic)) +
    geom_col() +
    geom_errorbar(aes(ymin = statistic - 1.96*std.error, 
                      ymax = statistic + 1.96*std.error,
                      width = 0.4)) +
    scale_y_continuous(limits = c(0,.8)) +
    geom_hline(yintercept = 0.5, color = "red", linetype = "dashed") +
    facet_wrap(~policy.condition) +
    theme_shom_alt() +
    theme(axis.text.x = element_text(angle = 90),
          axis.title.x = element_blank())
}

#bootstrap means
boot.dv <- function(policy.condition, dv) {
  boot.outcome <- function(d, i){
    d2 <- d[i,]
    return(mean(d2[[dv]]))
  }
  
  rbind(boot(dt[dt$policy == policy.condition & dt$treatment == "control",], boot.outcome, R = 5000) %>% tidy,
        boot(dt[dt$policy == policy.condition & dt$treatment == "cost",], boot.outcome, R = 5000) %>% tidy,
        boot(dt[dt$policy == policy.condition & dt$treatment == "benefit",], boot.outcome, R = 5000) %>% tidy,
        boot(dt[dt$policy == policy.condition & dt$treatment == "both",], boot.outcome, R = 5000) %>% tidy) %>%
    cbind(c("control", "cost", "benefit", "both"),
          policy.condition,
          dv) %>%
    as.data.frame() %>% 
    rename(
      exp.condition = `c(\"control\", \"cost\", \"benefit\", \"both\")`
    ) %>%
    mutate(
      policy.condition = case_when(
        policy.condition == "codeterm" ~ "Codetermination",
        policy.condition == "elections" ~ "Manager elections",
        policy.condition == "esops" ~ "ESOPs",
        policy.condition == "enviro" ~ "Fracking",
        policy.condition == "parent" ~ "Family leave"
      ) %>% 
        factor(.,
               c("Codetermination",
                 "Manager elections",
                 "ESOPs",
                 "Fracking",
                 "Family leave")),
      exp.condition = case_when(
        exp.condition == "control" ~ "Control",
        exp.condition == "cost" ~ "Cost",
        exp.condition == "benefit" ~ "Benefit",
        exp.condition == "both" ~ "Cost and benefit"
      ) %>%
        factor(.,
               c("Control",
                 "Cost",
                 "Benefit",
                 "Cost and benefit"))
    )
}

#load data
dt <- read_rds("08-reviews/experiment/data/clean-data-stacked-pol")

#set seed
set.seed(123456789)

#### **workplace democracy policies manipulation checks Table S3 ####
rbind(
  boot.dv("codeterm", "manip.check"),
  boot.dv("elections", "manip.check"),
  boot.dv("esops", "manip.check")
) -> plot.data

plot.data %>%
  transmute(
    `Policy condition` = policy.condition,
    `Experimental condition` = exp.condition,
    `Average correct answer` = round(statistic * 100, 2) %>% paste0("%"),
    `Standard error` = round(std.error * 100, 2) %>% paste0("%")
  ) %>%
  xtable::xtable() %>%
  print(include.rownames = FALSE)
